package top.szhome.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.szhome.framework.starter.db.support.SimpleServiceImpl;
import top.szhome.system.convert.RoleMenuConverter;
import top.szhome.system.dto.RoleMenuDTO;
import top.szhome.system.entity.RoleMenu;
import top.szhome.system.mapper.RoleMenuMapper;
import top.szhome.system.service.RoleMenuService;
import top.szhome.system.vo.RoleMenuVO;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 角色菜单 服务实现
 *
 * @author 唐加利
 * @date 2023/07/31
 **/
@AllArgsConstructor
@Service
@Slf4j
public class RoleMenuServiceImpl extends SimpleServiceImpl<RoleMenuMapper, RoleMenuConverter, RoleMenuDTO, RoleMenu, RoleMenuVO> implements RoleMenuService {

    @Override
    public List<Long> getMenusOfRole(Long roleId) {
        List<RoleMenu> menus = list(Wrappers.lambdaQuery(RoleMenu.class).eq(RoleMenu::getRoleId, roleId));
        return menus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
    }

    @Override
    public Map<Long, List<Long>> getMenusOfRoles(List<Long> roleIds){
        List<RoleMenu> menus = list(Wrappers.lambdaQuery(RoleMenu.class).in(RoleMenu::getRoleId, roleIds));
        return menus.stream().collect(Collectors.groupingBy(RoleMenu::getRoleId, Collectors.mapping(RoleMenu::getMenuId, Collectors.toList())));
    }

    @Override
    public List<Long> getRolesOfMenu(Long menuId) {
        List<RoleMenu> menus = list(Wrappers.lambdaQuery(RoleMenu.class).eq(RoleMenu::getMenuId, menuId));
        return menus.stream().map(RoleMenu::getRoleId).collect(Collectors.toList());
    }
}
